$(function () {
    var init = true; //第一次加载树之后点击根节点

    //加载树
    $("#west_khh_gsjg_tree").tree({
        loadFilter: function (rows) {
            return convert(rows);
        },
        onClick: function (node) {
            khh_search(node.id);
        },
        //新增修改删除数据后重新加载，并选中之前的节点
        onLoadSuccess: function (node, data) {
            var rootNode = $("#west_khh_gsjg_tree").tree("getRoot");
            $("#west_khh_gsjg_tree").tree("select", rootNode.target);
            if (init) {
                rootNode.target.click();
                init = false;
            }
        }
    });

    $("#khh_new").click(function () {
        khh_new();
    });
    $("#khh_mod").click(function () {
        khh_modify();
    });
    $("#khh_del").click(function () {
        khh_delete();
    });
    $("#btn_khh_search").click(function () {
        khh_search();
    });

});
/* //按公司开户行全称简称查询
  function gskhh_search() {
    $('#khh_grid').datagrid('load',
        {
             qc:$('#_qc').val(),
             jc:$('#_jc').val()
        }
    );
}*/

var khhWin;
//新增公司机构
function khh_new() {
    var node = $("#west_khh_gsjg_tree").tree("getSelected");
    khhWin = $.window({
        title: '添加开户行',
        width: 750,
        height: 450,
        cache: false,
        url: $.webPath() + '/organ/khh/khh_content?nbgsId=' + node.id,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: khh_saveItem
            }
        ]
    });
}
//修改公司机构
function khh_modify() {
    var khhId = $("#khh_grid").datagrid("getSelected").id;
    khhWin = $.window({
        title: '修改开户行',
        width: 750,
        height: 450,
        cache: false,
        url: $.webPath() + '/organ/khh/khh_content?khhId=' + khhId,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: khh_updateItem
            }
        ]
    });
}
//删除公司机构
function khh_delete() {
    var khhId = $("#khh_grid").datagrid("getSelected").id;
    $.messager.confirm("确认", "确定删除此开户行？", function (r) {
        if (r) {
            $.ajax({
                url: $.webPath() + "/organ/khh/delete_khh?khhId=" + khhId,
                type: "json",
                beforeSend: function () {
                    $("#khh_layout").mask();
                },
                success: function (data) {
                    $("#khh_layout").mask('hide');
                    data = $.parseJSON(data);
                    if (data.statusCode == '200') {
                        $('#khh_grid').datagrid('reload');
                        $.messager.show({
                            title: '提示框',
                            msg: data.message,
                            timeout: 3000,
                            showType: 'slide'
                        });
                    } else {
                        $.messager.alert('提示', data.message, 'info');
                    }
                }
            });
        }
    })
}
//保存
function khh_saveItem() {
    $(khhWin).mask();
    var url = $.webPath() + '/organ/khh/save_khh';
    $('#khhForm').form('submit', {
        url: url,
        onSubmit: function () {
            var valid = $(this).form('validate');
            if (!valid)
                $(khhWin).mask('hide');
            return valid;
        },
        success: function (data) {
            $(khhWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(khhWin).window("close");
                $('#khh_grid').datagrid('reload');
                $.messager.show({
                    title: '提示框',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}

function khh_updateItem() {
    $(khhWin).mask();
    var url = $.webPath() + '/organ/khh/update_khh';
    $('#khhForm').form('submit', {
        url: url,
        onSubmit: function () {
            var valid = $(this).form('validate');
            if (!valid)
                $(khhWin).mask('hide');
            return valid;
        },
        success: function (data) {
            $(khhWin).mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(khhWin).window("close");
                $('#khh_grid').datagrid('reload');
                $.messager.show({
                    title: '提示框',
                    msg: obj.message,
                    timeout: 3000,
                    showType: 'slide'
                });
            } else {
                $.messager.alert('提示', obj.message, 'error');
            }
        }
    });
}

//公司机构树查询
function khh_search(id) {
    var qc=$('#qc').val();
    var jc=$('#jc').val();
    var options = $.extend(getKhhGridOption(),
        {queryParams: {nbgsId: id,qc:qc,jc:jc}});
    $('#khh_grid').datagrid(options);
}
//根据不同的type得到不同的表头
function getKhhGridOption() {
    var options = {
        url: $.webPath() + "/organ/khh/khhList",
        toolbar: "#khh_toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function (target) {
            $("#khh_del").hide();
            $("#khh_mod").hide();
        },
        onClickRow: function (rowIndex, rowData) {
            $("#khh_del").show();
            $("#khh_mod").show();
        },
        onDblClickCell: function (rowIndex, field, value) {
            if (field == "nbgs_qc")
                khh_modify();
        }
    };
    var columns = {columns: [
        [
            {field: 'nbgs_qc', title: '公司名称',align: 'center', width: 50},
            {field: 'qc', title: '帐户名称',align: 'center', width: 40},
            {field: 'jc', title: '帐户简称',align: 'center', width: 40},
            {field: 'zh', title: '帐号',align: 'center', width: 30},
            {field: 'zhlx_z', title: '帐户类型',align: 'center', width: 30},
            {field: 'kmdm', title: '科目代码',align: 'center', width: 30},
            {field: 'gysfzhsdm', title: '供应商辅助核算代码',align: 'center', width: 30},
            {field: 'khfzhsdm', title: '客户辅助核算代码',align: 'center', width: 30},
            {field: 'drzh', title: '导入字号',align: 'center', width: 30},
            {field: 'lxdh', title: '联系人电话',align: 'center', width: 50}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}

